package org.piaohao.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.piaohao.model.Vehicle;
import org.piaohao.util.string.JsonUtil;
import org.piaohao.util.string.UUIDGenerator;

/**
 * Created on : 2013-5-4, 17:22:37
 *
 * @author piaohao
 */
public class VehicleController extends Controller {

    public static final String moduleName = "车辆档案模块";

    public void index() {
        render("/pages/vehicle/vehicle.jsp");
    }

    public void list() {
        try {
            Integer start = getParaToInt("start");
            Integer limit = getParaToInt("limit");
            String orgId = (String) getSessionAttr("orgId");
            List<Vehicle> vehicles = null;
            Long total = 0L;
            if (orgId.equals("0")) {
                vehicles = Vehicle.dao.find("select * from t_vehicle limit ?,?", start, limit);
                total = Db.queryLong("select count(*) from t_vehicle");
            } else {
                vehicles = Vehicle.dao.find("select * from t_vehicle where org_id=? limit ?,?", orgId, start, limit);
                total = Db.queryLong("select count(*) from t_vehicle where org_id=?", orgId);
            }
            renderJson(JsonUtil.printGrid(total, vehicles));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void add() {
        try {
            Vehicle vehicle = getModel(Vehicle.class);
            String para1 = getPara("veh_reg_date");
            DateFormat df = new SimpleDateFormat("yy-MM-dd");
            Date date = df.parse(para1);
            vehicle.set("veh_reg_date", new Timestamp(date.getTime()));
            if (vehicle.getStr("id") == null || vehicle.getStr("id").trim().equals("")) {
                vehicle.set("id", UUIDGenerator.getUUID());
                vehicle.save();
            }
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void update() {
        try {
            Vehicle vehicle = getModel(Vehicle.class);
            String para1 = getPara("veh_reg_date");
            DateFormat df = new SimpleDateFormat("yy-MM-dd");
            Date date = df.parse(para1);
            vehicle.set("veh_reg_date", new Timestamp(date.getTime()));
            vehicle.update();
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void delete() {
        try {
            String[] idArr = getParaValues("ids");
            for (int i = 0; i < idArr.length; i++) {
                Vehicle.dao.deleteById(idArr[i]);
            }
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void showDetail() {
        try {
            String id = getPara(0);
            Record vehicle = Db.findFirst("select v.*,o.name orgName from t_vehicle v left join t_organization o on v.org_id=o.id where v.id=?", id);
            setAttr("vehicle", vehicle);
            render("/pages/vehicle/detail.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }
}
